#include "nw_order.h"
#include "string.h"
#include "nw_client_pal.h"
#include "rtc.h"
#include "utility.h"
#include "stdio.h"

static void init_tradeno(struct t_nw_order *order)
{
	 //生成交易流水号
	
	  T_TimeFmt time_now;
	  rtc_get_current_time(&time_now);
	
	  memcpy(order->tradeno,order->devnum,8);
	  
	 char serialnum[17] = {0};
	 sprintf(serialnum,"%02d%02d%02d%02d%02d%02d%04d",time_now.year,time_now.month,time_now.date,time_now.hour,time_now.min,time_now.sec,1);//年月日时分秒+枪号
	 
	 char bcd[17] = {0};
	 ASC_to_BCD((uint8 *)bcd,(uint8 *)serialnum,strlen(serialnum));
	 
	 memcpy(order->tradeno+8,bcd,8);  
}
void reset_order(struct t_nw_order *order)
{
	 memset(order,0,sizeof(struct t_nw_order));
}
void init_order(struct t_nw_order *order,struct t_cli_costmode *clicostmode,uint8 type,void *arg)
{
	 order->orderstate = 1; //订单状态为可用
	 order->startway = type;//启动方式
	
	 order->costmode = clicostmode;
	
	 order->business_type = 0x01;//义务类型为充电量
	 order->cal_type = 2;//充电量
	 order->sharetime_flag = 0;//分时计费标识为分时
	
	 pal_callback_get_time_cp56(order->time_begin);//开始充电时间
	
	 if(E_START_BYREMOTE == type){//通过远程启动
		 struct t_down_remote_start *premote = (struct t_down_remote_start*)arg;
		 memcpy(order->devnum,premote->devnum,8);
		 order->gunno = premote->gunno;
		 order->user_remain  = premote->remain;//余额
		 memcpy(order->userid,premote->userid,8);//用户id
		 order->type = premote->type;//充电方式
		 order->value = premote->value;//充电参数
		 memcpy(order->order,premote->orderno,8);//订单号
		 
		 memset(order->cardno,0xff,8);//物理卡号
     memset(order->vin,0xff,32);//车辆的vin编号
	 }else if(E_START_BYCARD == type){
		 struct t_down_authdata *pcard = (struct t_down_authdata *)arg;
		 memcpy(order->devnum,pcard->devnum,8);
		 order->gunno = pcard->gunno;
		 order->user_remain  = pcard->reamin_money;//余额
		 memset(order->userid,0xff,8);//用户id
		 //order->type = pcard->type;//充电方式
		 //order->value = pcard->value;//充电参数
		 memcpy(order->order,pcard->orderno,8);//订单号
		 
		 memcpy(order->cardno,pcard->cardno,8);//物理卡号
     memcpy(order->vin,pcard->carvin,32);//车辆的vin编号	
     order->remainkm = pcard->remain_km;// 剩余里程 精确到小数点后两位
	   order->remainpower = pcard->avalib_power;//可充电量 BIN 码 4Byte 精确到小数点后两位
     order->remaincnt = pcard->avalib_cnt;// 剩余次数 BIN 码 4Byte 精确到小数点后两位
	 }
	
	 init_tradeno(order); 	
}
void order_updatepower(struct t_nw_order *order,uint32 power)//功率变化0.01kwh就更新
{	
	 if(power <=order->lastpower)
	 {
		  return;
	 }
	 printf("------------------order_updatepower begin------------------\r\n");
	 
	 T_TimeFmt time_now;
	 rtc_get_current_time(&time_now);//拿取当前时间
	  
	 uint32 flag = get_timeflag(order->costmode,time_now.hour,time_now.min);
	 if(!flag)
	 { 
		  return ;
	 }
	 printf("flag = %d\r\n",flag);
	 struct t_order_elecmsg *pelecmsg = (order->elecmsg+flag-1);
	
	 //计算用电量
	 if(!order->first_update_flag){//第一次更新
		  order->lastpower = power;
		  order->first_update_flag = 1;
		  return ;
	 }else{
	    pelecmsg->power = pelecmsg->power + (power - order->lastpower);//时段用电量 保留小数点后两位 0.01kwh
	 }
	 order->lastpower = power;
	 printf("update power = %d\r\n", pelecmsg->power);
	 
	 //计算电费
	 uint32 elecpcs = get_elecpcsbytime(order->costmode,time_now.hour,time_now.min);//单位是保留小数点后五位的
	 pelecmsg->eleccost = (elecpcs * pelecmsg->power)/100000 ;
	 
	 printf("elecpcs = %d\r\n",elecpcs);
	 printf("eleccost = %d\r\n", pelecmsg->eleccost);
	 
	 //计算服务费
	 uint32 servepcs = get_servepcsbytime(order->costmode,time_now.hour,time_now.min);//单位是保留小数点后五位的 
	 pelecmsg->serve_cost = (servepcs * pelecmsg->power) /100000;
	 
	 printf("servepcs = %d\r\n",servepcs);
	 printf("pelecmsg->serve_cost = %d\r\n",pelecmsg->serve_cost);
	 
	 printf("------------------order_updatepower end------------------\r\n");

}

uint16 get_order_business_type(struct t_nw_order *order)
{
	  return order->business_type;
}
uint16 get_order_caltype(struct t_nw_order *order)
{
	  return order->cal_type;
}
uint8 get_order_sharetime_flag(struct t_nw_order *order)
{
	 return order->sharetime_flag;
}
void order_set_endreason(struct t_nw_order *order,uint8 endreason)
{
	order->endreason = endreason;
}
uint16 order_get_endreason(struct t_nw_order *order)//拿取结束原因
{
	return order->endreason;
}
void get_order_tradeno(struct t_nw_order *order,uint8 *tradeno)
{
	memcpy(tradeno,order->tradeno,16);
}
uint8 get_order_startway(struct t_nw_order *order)
{
	return order->startway;
}

void get_order_userid(struct t_nw_order *order,uint8 *user_id)
{
	memcpy(user_id,order->userid,8);	
}
void get_order_cardno(struct t_nw_order *order,uint8 *card_no)
{
	memcpy(card_no,order->cardno,8);	
}
void get_order_orderno(struct t_nw_order *order,uint8 *order_id)
{
  memcpy(order_id,order->order,8);	
}
void get_order_begintime(struct t_nw_order *order,uint8 *time)
{
	memcpy(time,order->time_begin,7);
}
void get_order_endtime(struct t_nw_order *order,uint8 *time)
{
	 if(order->endreason){
		 pal_callback_get_time_cp56(time);
	 }
	 else{
		 memset(time,0xff,7);
	 }	 
}
uint32 get_order_time_pcs(struct t_nw_order *order,uint8 flag)
{
	 uint32 elecpcs = get_elecpcs(order->costmode,flag);//电费单价
	 //printf("get_order_time_pcs = %d flag = %d\r\n",elecpcs,flag);
	 return elecpcs;
}
uint32 get_order_time_power(struct t_nw_order *order,uint8 flag)
{
	 if(flag>4)
	 {
		  return 0;
	 }
	 uint32 power = order->elecmsg[flag-1].power;
	 //printf("get_order_time_power :%d flag = %d\r\n",power,flag);
	 return power;
}
uint32 get_order_total_power(struct t_nw_order *order)
{
	uint32 total = 0;
	for(uint32 i = 0;i<4;i++)
	{
		 total = total + order->elecmsg[i].power;
	}
	//printf("get_order_total_power :%d\r\n",total);
	return total;
}

uint32 get_order_time_eleccost(struct t_nw_order *order,uint8 flag)//拿取某个时点的电费
{
	 if(flag>4)
	 {
		  return 0;
	 }
	 uint32 eleccost = order->elecmsg[flag-1].eleccost;
	 //printf("get_order_time_eleccost = %d flag = %d\r\n",eleccost,flag);
	 return eleccost;
}
uint32 get_order_total_eleccost(struct t_nw_order *order)
{
	uint32 total = 0;
	for(uint32 i = 0;i<4;i++)
	{
		 total = total + order->elecmsg[i].eleccost;
	}
	//printf("get_order_total_eleccost = %d\r\n",total);
	return total;
}
uint32 get_order_time_servepcs(struct t_nw_order *order,uint8 flag)//拿取某个时段的充电服务费单价
{
	 uint32 pcs = get_sercepcs(order->costmode,flag);
	 //printf("get_order_time_servepcs %d flag:%d\r\n",pcs,flag);
	 return pcs;
}
uint32 get_order_time_servecost(struct t_nw_order *order,uint8 flag)//拿取某个时段的服务费
{
	 if(flag>4){
		  return 0;
	 }
	 uint32 cost = order->elecmsg[flag-1].serve_cost;
	 //printf("get_order_time_servecost %d flag:%d",cost,flag);
	 return cost;
}

uint32 get_order_total_servercost(struct t_nw_order *order)//拿取总的服务费
{
	uint32 total = 0;
	for(uint32 i = 0;i<4;i++){
		 total = total + order->elecmsg[i].serve_cost;
	}
	//printf("get_order_total_servercost = %d\r\n",total);
	return total;
}

uint32 get_order_reserve_pcs(struct t_nw_order *order,uint8 flag)//预约费单价
{
	 if(flag>4){
		  return 0;
	 }
	 return get_reservepcs(order->costmode,flag);
}

uint32 get_order_total_reservecost(struct t_nw_order *order)//拿取总的预约费
{
	uint32 total = 0;
	for(uint32 i = 0;i<4;i++){
		 total = total + order->elecmsg[i].reserve_cost;
	}
	return total;
}

uint32 get_order_position_pcs(struct t_nw_order *order,uint8 flag)//占位费单价
{
	 if(flag>4){
		  return 0;
	 }
	 return get_positionpcs(order->costmode,flag);
}
uint32 get_order_total_positioncost(struct t_nw_order *order)
{
	uint32 total = 0;
	for(uint32 i = 0;i<4;i++){
		 total = total + order->elecmsg[i].position_cost;
	}
	return total;
}
uint16 get_order_totaltime(struct t_nw_order *order)//总耗时
{
	pal_callback_get_time_cp56(order->time_end);//接收充电时间
	
	uint32 beginsec = gettotalsec(order->time_begin);
	uint32 endsec = gettotalsec(order->time_end);
	
	uint32 sec = endsec-beginsec;
	
	uint16 min = sec/60;
	//printf("get_order_totaltime = %d\r\n",min);
	return min;//返回分钟
}
uint16 get_order_remaintime(struct t_nw_order *order)//预计剩余充电时间
{
	return order->remaintime;
}
uint32 get_order_totalcost(struct t_nw_order *order)//总费用
{
	uint32 total = 0;
	for(uint32 i = 0;i<4;i++){
		 total += order->elecmsg[i].eleccost;
		 total += order->elecmsg[i].serve_cost;
		 total += order->elecmsg[i].position_cost;
		 total += order->elecmsg[i].reserve_cost;	
	}
	//printf("get_order_totalcost = %d\r\n",total);
	return total;
}
uint32 get_order_money_afterpay(struct t_nw_order *order)//获取扣款后钱包月
{
	 if(E_START_BYREMOTE == order->startway)
	 {
		  return 0xffffffff;
	 }
	 return 0;
}
uint32 get_order_consume_pcs(struct t_nw_order *order)//获取消费单价
{
	 return 0xffffffff;
}
uint32 get_order_consume_money(struct t_nw_order *order)//获取消费金额
{
	 return 0xffffffff;
}
uint32 get_order_user_remain(struct t_nw_order *order)//获取钱包余额
{
	 return order->user_remain;
}	
void  get_order_costmode_id(struct t_nw_order *order,uint8 *id)//获取计费模型id
{
	 get_costmode(order->costmode,id);
}
